import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:signature/signature.dart';

/// todo: 手写板
class SignPrint extends StatefulWidget {
  final SignatureController signatureController;
  final Function(dynamic)? singOK;
  const SignPrint({Key? key,required this.signatureController, this.singOK}) : super(key: key);

  @override
  _SignPrintState createState() => _SignPrintState();
}

class _SignPrintState extends State<SignPrint> {
  bool tmpIsEmpty = false;
  bool sinOK = false;

  /// todo：导出画板图片
  exImg(){
    widget.singOK!(widget.signatureController.toImage());
  }

  /// todo：清空画板
  cleanSign(){
    widget.signatureController.clear();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    widget.signatureController.addListener(() {
      if (widget.signatureController.value.length > 0) {
        tmpIsEmpty = true;
      } else {
        tmpIsEmpty = false;
      }
      print(widget.signatureController.value);
      setState(() {

      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    widget.signatureController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 300,
      child: RotatedBox(
        quarterTurns: 0,
        child:  Stack(
          children: [
            Signature(
              controller: widget.signatureController,
              width: Get.width,
              height: 300,
              backgroundColor: Colors.white,
            ),
            Positioned(
              right: 10,
              bottom: 10,
              child: !tmpIsEmpty ? SizedBox() :
                  Row(
                    children: [
                      MaterialButton(
                        onPressed: (){
                          cleanSign();
                        },
                        child: Text("清除手写"),
                        color: Colors.grey[200],
                        elevation: 0.0,
                      ),
                      SizedBox(width: 10,),
                      MaterialButton(
                        onPressed: (){
                          exImg();
                        },
                        child: Text("完成手写"),
                        color: Colors.grey[200],
                        elevation: 0.0,
                      ),
                    ],
                  ) ,
            ),
          ],
        ),
      ),
    );
  }
}
